﻿using ReactiveUI;
using WinForm.ReactiveUI.UseSourceGenerators.ViewModels;

namespace WinForm.ReactiveUI.UseSourceGenerators.Views
{
    public partial class LoginView : Form, IViewFor<LoginViewModel>
    {
        public LoginView()
        {
            InitializeComponent();

            ViewModel = new LoginViewModel();

            this.WhenActivated(d =>
            {
                // 绑定属性
                d(this.Bind(ViewModel, vm => vm.Username, v => v.textBoxUsername.Text));
                d(this.Bind(ViewModel, vm => vm.Password, v => v.textBoxPassword.Text));
                // 绑定命令
                d(this.BindCommand(ViewModel, vm => vm.LoginCommand, v => v.buttonLogin));
            });
        }

        public LoginViewModel? ViewModel { get; set; }
        object? IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (LoginViewModel?)value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(@$"usename = {ViewModel?.Username}, password = {ViewModel?.Password}");
        }
    }
}
